package com.handong.handserialprogram.module.cofigring;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.ColorContant;
import com.handong.handserialprogram.model.entities.FootRingNewEntity;
import com.handong.handserialprogram.model.status.FootRingStatus;

import java.util.List;

/**
 * Created by userKey on 2018/6/21.
 */

public class ConfigRingAdaper  extends RecyclerView.Adapter<ConfigRingAdaper.ConfigRingViewHolder>{

    private List<FootRingNewEntity> mFootRingList;
    private Context context;

    private CallbackListener callbackListener;

    public interface CallbackListener {
        void onItemClick(FootRingNewEntity entity);
        void onConfigClick(FootRingNewEntity entity);
    }

    public ConfigRingAdaper( Context context,List<FootRingNewEntity> footRingList,CallbackListener callbackListener) {
        this.mFootRingList = footRingList;
        this.context=context;
        this.callbackListener=callbackListener;
    }

    @Override
    public ConfigRingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_ring_list_item, parent, false);
        return new ConfigRingViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ConfigRingViewHolder holder, int position) {
        FootRingNewEntity entity = mFootRingList.get(position);
        holder.mHolderEntity = entity;
        holder.ringID.setText(entity.getRingID());
        holder.ringActualDistance.setText(entity.getActualRingDistance() + "m");
        holder.ringSetDistance.setText(entity.getSetRingDistance() + "m");
        if (entity.getStatus() == FootRingStatus.ESCAPE) {
            holder.ringActualDistance.setTextColor(ColorContant.PERSON_ESCAPE);
        } else {
            holder.ringActualDistance.setTextColor(Color.parseColor("#6198FE"));
        }

        setBtnState(holder,R.drawable.icon_ring_working,"配置",ColorContant.MISSION_NORMAL,true);

    }
    private void setBtnState(ConfigRingViewHolder holder, int iconID, String des, int color, boolean isEnabled){
        GradientDrawable drawable = (GradientDrawable) holder.ringStatus.getBackground();
        holder.ringIcon.setImageResource(iconID);
        holder.ringStatus.setText(des);
        drawable.setColor(color);
        holder.mActivateLayout.setEnabled(isEnabled);
    }
    @Override
    public int getItemCount() {
        return mFootRingList.size();
    }

    class ConfigRingViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private FootRingNewEntity mHolderEntity;
        private TextView ringID, ringSetDistance, ringActualDistance, ringStatus;
        private FrameLayout mActivateLayout;
        private ImageView ringIcon;

        public ConfigRingViewHolder(View itemView) {
            super(itemView);

            ringIcon = (ImageView) itemView.findViewById(R.id.ringItemIcon);
            ringID = (TextView) itemView.findViewById(R.id.ringItemID);
            ringSetDistance = (TextView) itemView.findViewById(R.id.ringItemSetDistance);
            ringActualDistance = (TextView) itemView.findViewById(R.id.ringItemActualDistance);
            ringStatus = (TextView) itemView.findViewById(R.id.ringItemBtnSet);
            mActivateLayout = (FrameLayout) ringStatus.getParent();
            mActivateLayout.setOnClickListener(this);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ringItemBtnSetLayout://配置
                    callbackListener.onConfigClick(mHolderEntity);
                    break;
                case R.id.ringItemContainer:
                    callbackListener.onItemClick(mHolderEntity);
                    break;
            }
        }
    }
}
